package com.shop.service.Impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.mapper.*;
import com.shop.pojo.*;
import com.shop.service.DtsGoodsAttributeService;
import com.shop.service.DtsGoodsProductService;
import com.shop.service.DtsGoodsService;
import com.shop.service.DtsGoodsSpecificationService;
import com.shop.vo.GoodsAllinone;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName DtsGoodsServiceImpl
 * @Author xiao宋
 * @Date 21:30  2022/10/13
 * @Version 1.0
 * @Description TODO
 */
@Service
public class DtsGoodsServiceImpl implements DtsGoodsService {
    @Autowired
    DtsGoodsMapper goodsMapper;
    @Autowired
    DtsCategoryMapper categoryMapper;
    @Autowired
    DtsGoodsProductMapper productMapper;
    @Autowired
    DtsGoodsSpecificationMapper specificationMapper;
    @Autowired
    DtsGoodsAttributeMapper goodsAttributeMapper;
    @Autowired
    DtsGoodsAttributeService attributeService;
    @Autowired
    DtsGoodsProductService productService;
    @Autowired
    DtsGoodsSpecificationService specificationService;

    @Override
    public long getTotal() {
        LambdaQueryWrapper<DtsGoods> w = new LambdaQueryWrapper<>();
        w.eq(DtsGoods::getDeleted, false);
        Integer total = goodsMapper.selectCount(w);
        return total;
    }

    @Override
    public int findValidGoodsByBrandId(Integer brandId) {
        QueryWrapper<DtsGoods> w = new QueryWrapper<>();
        w.eq("brand_id", brandId);
        w.eq("deleted", false);
        List list = goodsMapper.selectList(w);
        return list.size();

    }

    @Override
    public Map<String, Object> findByPage(Integer page, Integer limit, String goodsSn, String name, String order, String sort, List<Integer> brandIds) {
        IPage<DtsGoods> iPage = new Page<>(page, limit);
        QueryWrapper<DtsGoods> w = new QueryWrapper<>();
        if (!StringUtils.isEmpty(goodsSn)) {
            w.like("goods_sn", goodsSn);
        }
        if (!StringUtils.isEmpty(name)) {
            w.like("name", name);
        }
        w.eq("deleted", false);
        if ("desc".equalsIgnoreCase(order)) {
            w.orderByDesc(sort);
        } else {
            w.orderByAsc(sort);
        }
        goodsMapper.selectPage(iPage, w);
        Map<String, Object> data = new HashMap<>();
        data.put("total", iPage.getTotal());
        data.put("items", iPage.getRecords());

        return data;
    }

    @Override
    public void insert(GoodsAllinone goodsAllinone) {
        DtsGoods goods = goodsAllinone.getGoods();
        Integer count;
//        新增商品
        LambdaQueryWrapper<DtsGoods> w = new LambdaQueryWrapper<>();
        w.eq(DtsGoods::getGoodsSn, goods.getGoodsSn());
        count = goodsMapper.selectCount(w);
        if (count > 0) {
            throw new RuntimeException("该商品编号已经存在");
        }
//        LambdaQueryWrapper<DtsGoods> wrapper = new LambdaQueryWrapper<>();
        count = goodsMapper.selectCount(new LambdaQueryWrapper<DtsGoods>().eq(DtsGoods::getName, goods.getName()));
        if (count > 0) {
            throw new RuntimeException("该商品名称已经存在");
        }
        goods.setAddTime(new Date());
        goods.setUpdateTime(new Date());
        goods.setDeleted(false);
        goodsMapper.insert(goods);

        DtsGoodsProduct[] products = goodsAllinone.getProducts();
        for (DtsGoodsProduct product : products) {
            product.setGoodsId(goods.getId());
            productMapper.insert(product);
        }
        DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
        for (DtsGoodsAttribute attribute : attributes) {
            attribute.setGoodsId(goods.getId());
            goodsAttributeMapper.insert(attribute);
        }


        DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();
        for (DtsGoodsSpecification specification : specifications) {
            specification.setGoodsId(goods.getId());
            specificationMapper.insert(specification);
        }

    }

    @Override
    public Map<String, Object> detail(Integer goodsId) {
//        商品属性
        DtsGoods goods = goodsMapper.selectById(goodsId);
        LambdaQueryWrapper<DtsGoodsAttribute> w = new LambdaQueryWrapper<>();
        w.eq(DtsGoodsAttribute::getGoodsId, goods);
        w.eq(DtsGoodsAttribute::getDeleted, false);
        List<DtsGoodsAttribute> dtsGoodsAttributes = goodsAttributeMapper.selectList(w);
//        商品规格
        LambdaQueryWrapper<DtsGoodsSpecification> w2 = new LambdaQueryWrapper();
        w2.eq(DtsGoodsSpecification::getGoodsId, goodsId);
        w2.eq(DtsGoodsSpecification::getDeleted, false);
        List<DtsGoodsSpecification> dtsGoodsSpecifications = specificationMapper.selectList(w2);
//        库存
        LambdaQueryWrapper<DtsGoodsProduct> w3 = new LambdaQueryWrapper<>();
        w3.eq(DtsGoodsProduct::getGoodsId, goodsId);
        w3.eq(DtsGoodsProduct::getDeleted, false);
        List<DtsGoodsProduct> dtsGoodsProducts = productMapper.selectList(w3);

        Integer categoryId = goods.getCategoryId();

        DtsCategory category = categoryMapper.selectById(categoryId);
        Integer pid = category.getPid();
        Map<String, Object> data = new HashMap<>();
        data.put("categoryIds", Arrays.asList(pid, categoryId));
        data.put("attributes", dtsGoodsAttributes);
        data.put("goods", goods);
        data.put("specifications", dtsGoodsSpecifications);
        data.put("products", dtsGoodsProducts);

        return data;
    }

    @Override
    public void update(GoodsAllinone goodsAllinone) {
        DtsGoods goods = goodsAllinone.getGoods();
        goods.setUpdateTime(new Date());
//        更新商品表
        goodsMapper.updateById(goods);
//        删除属性
        goodsAttributeMapper.deleteById(goods.getId());
//        删除规格
        specificationService.deleteById(goods.getId());
//        删除库存
        productService.deleteById(goods.getId());
//        添加新的商品属性
        DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
        for (DtsGoodsAttribute attribute : attributes) {
            attribute.setGoodsId(goods.getId());
            attributeService.insert(attribute);
        }
//        添加新的规格
        for (DtsGoodsSpecification specification : goodsAllinone.getSpecifications()) {
            specification.setGoodsId(goods.getId());
            specificationService.insert(specification);
        }
//        添加新的库存信息
        for (DtsGoodsProduct product : goodsAllinone.getProducts()) {
            product.setGoodsId(goods.getId());
            productService.insert(product);
        }
    }

    /**
     * todo 没有写删除图片的事
     * @param goods
     */
    @Override
    public void delete(DtsGoods goods) {
//        删除商品属性
        attributeService.deleteByGoodsId(goods.getId());
        specificationService.deleteById(goods.getId());
        productService.deleteById(goods.getId());
        goodsMapper.deleteById(goods.getId());

    }


}
